package org.example;

import java.util.Objects;
import java.util.Scanner;

/*
 * 用户类
 */

public class User {

    /*
     * 用户的钱
     */
    static double myMoney;

    /*
     * 用户登录界面
     */
    void userMenu(){

        /*
         * 用户密码输入
         */
        Painter.paintingSpace();
        System.out.print("请输入用户密码（输入#退出登录）：");

        /*
         * 接收密码并处理异常情况
         */
        Check check = new Check();
        String password;
        Scanner reader = new Scanner(System.in);
        password = reader.next();
        if(check.isOut(password)){
            Supermarket.supermarketMainMenu();
        }

        /*
         * 登陆成功
         */
        if(Objects.equals(password, "123456789")){

            Painter.paintingSpace();
            System.out.println("----------用户登录成功----------");
            Painter.paintingSpace();
            System.out.println("----------欢迎来到蓝旭大超市----------");
            Painter.paintingSpace();
            System.out.println("用户操作：1.查看商品 2.购买商品 3.我的存款 4.钱++(ψ(｀∇´)ψ！) 5.退出用户登录");
            Painter.paintingSpace();
            System.out.print("请输入您想进行操作的编号：");

            /*
             * 进入用户主界面并将钱置为0
             */
            User.setMyMoney();
            userFunction();

        }
        else{

            /*
             * 登录失败
             */
            Painter.paintingSpace();
            System.out.println("密码错误,请检查后重新输入");
            userMenu();

        }

    }

    /*
     * 用户功能主界面
     */
    static void userFunctionMenu(){
        Painter.paintingNSpace();
        System.out.println("********************************************************");
        Painter.paintingSpace();
        System.out.println("欢迎来到蓝旭大超市");
        Painter.paintingSpace();
        System.out.println("用户操作：1.查看商品 2.购买商品 3.我的存款 4.钱++(ψ(｀∇´)ψ！) 5.退出用户登录");
        Painter.paintingSpace();
        System.out.print("请输入您想进行操作的编号：");
        /*
         * 用户功能界面
         */
        userFunction();
    }

    /*
     * 用户功能界面
     */
    static void userFunction(){

        /*
         * 接收操作数并处理非数字字符异常
         */
        Check checker = new Check();
        Scanner reader = new Scanner(System.in);
        String temp = reader.next();
        while(checker.isNotIntegerNum(temp)){
            Painter.paintingSpace();
            System.out.print("请您输入正确的操作数:");
            temp = reader.next();
        }
        int userFunctionNum = Integer.parseInt(temp);

        /*
         * 执行操作数对应的操作并处理操作数越界异常
         */
        while(userFunctionNum!=5){
            switch (userFunctionNum){
                case 1 -> {
                    Supermarket.displayInventory();
                    userFunctionMenu();
                }
                case 2 -> {
                    userFunctionTwo();
                    userFunctionMenu();
                }
                case 3 -> {
                    userFunctionThree();
                    userFunctionMenu();
                }
                case 4 -> {
                    userFunctionFour();
                    userFunctionMenu();
                }
                default -> {
                    Painter.paintingNSpace();
                    System.out.println("请您输入正确的操作");
                    userFunctionMenu();
                }
            }

            /*
             * 处理非数字字符异常
             */
            temp = reader.next();
            if(checker.isNotIntegerNum(temp)){
                Painter.paintingSpace();
                System.out.println("请您输入正确的操作数！");
            }
            userFunctionNum = Integer.parseInt(temp);
        }

        /*
         * 用户退出，返回超市主界面
         */
        Painter.paintingSpace();
        System.out.println("用户退出成功");
        Supermarket.supermarketMainMenu();

    }

    /*
     * 用户功能二：购买商品
     */
    static void userFunctionTwo(){
        /*
         * 询问购买商品名称，并对不存在的异常进行处理
         */
        Scanner reader = new Scanner(System.in);
        Supermarket.displayInventory();
        Painter.paintingSpace();
        System.out.print("请问您想购买哪种商品(名称)：");
        String purchaseProductName = reader.next();
        boolean isExist = false;
        int purchaseIndex = 0;
        for(Product product : Supermarket.inventory){
            if(product.name.equals(purchaseProductName)){
                isExist = true;
                purchaseIndex = Supermarket.inventory.indexOf(product);
                break;
            }
        }
        Painter.paintingNSpace();
        if(!isExist){
            System.out.println("真是抱歉呐！我们店里没有该商品。");
            userFunctionMenu();
        }

        /*
         * 购买商品
         */
        else{
            System.out.printf("请问您想购买%s多少？\n",purchaseProductName);
            Painter.paintingSpace();
            System.out.print("数量：");

            /*
             * 处理购买数量非数字字符异常
             */
            Check checker = new Check();
            String temp = reader.next();
            while(checker.isNotIntegerNum(temp)){
                Painter.paintingSpace();
                System.out.print("请输入正确的数量：");
                temp = reader.next();
            }
            int purchaseNum = Integer.parseInt(temp);

            /*
             * 处理和购买数量异常和商品未删除异常
             */
            int realNum = Supermarket.inventory.get(purchaseIndex).quantity;
            if(purchaseNum<0){
                Painter.paintingSpace();
                System.out.println("真的吗？倒贴？我很乐意呐！");
                userFunctionMenu();
            }
            else if(purchaseNum==0){
                Painter.paintingSpace();
                System.out.println("Nothing but air!请您正常一点！");
                userFunctionMenu();
            }
            else if(realNum==0){
                Painter.paintingSpace();
                System.out.println("真的抱歉呐！我们忘记把该商品从货架上删除。");
                userFunctionMenu();
            }

            /*
             * 成功购买，处理商品数量不足的异常
             */
            else if(purchaseNum>realNum){
                Painter.paintingSpace();
                System.out.println("真是抱歉呐！我们店里该商品的数量不够。");
                Painter.paintingSpace();
                System.out.println("您可以选择把存货全部拿走");
                Painter.paintingSpace();
                System.out.print("要还是不要(yes、no)：");
                String yesOrNo = reader.next();
                if(yesOrNo.equals("yes")){
                    User.purchase(purchaseIndex,Supermarket.inventory.get(purchaseIndex).quantity);
                }
                else if(yesOrNo.equals("no")){
                    Painter.paintingSpace();
                    System.out.println("好吧，您可以看看别的商品。");
                    userFunctionMenu();
                }
                else{
                    Painter.paintingSpace();
                    System.out.println("请您检查您的拼写是否正确。");
                }
            }
            else{
                User.purchase(purchaseIndex,purchaseNum);
            }
        }
    }

    /*
     * 用户功能三：查看我的存款
     */
    static void userFunctionThree(){
        Painter.paintingSpace();
        System.out.println("(´థ౪థ ) ☞我的存款："+myMoney+"元");
    }

    /*
     * 用户功能四：钱++功能
     */
    static void userFunctionFour(){

        /*
         * 正确打出“我爱蓝旭”，钱+1000，打错了我相信你不是故意的
         */
        Scanner reader = new Scanner(System.in);
        Painter.paintingSpace();
        System.out.print("请您正确拼写出“我爱蓝旭”，之后您的存款就会+1000元:");
        String word = reader.next();
        Painter.paintingNSpace();
        if(word.equals("我爱蓝旭")){
            System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$钱++啦！$$$$$$$$$$$$$$$$$$$$$$$$");
            myMoney += 1000;
        }
        else{
            System.out.println("嘿，兄弟！我相信你不是故意的(⁎⚈᷀᷁ᴗ⚈᷀᷁⁎)再来一次吧！");
            userFunctionFour();
        }

    }

    /*
     * 用户购买功能
     */
    static void purchase(int index,int quantity){
        /*
         * 计算购买需要的钱数，并对钱够与不够进行处理
         */
        double totalMoney = quantity*Supermarket.inventory.get(index).price;
        if(totalMoney>myMoney){
            Painter.paintingSpace();
            System.out.println("嘿！你的钱不够哦！快去钱++!");
            userFunctionMenu();
        }
        else{
            myMoney -= totalMoney;
            Supermarket.inventory.get(index).quantity -= quantity;
            Painter.paintingSpace();
            System.out.printf("购买成功！我现在剩余：%.2f元\n",myMoney);
        }
    }

    /*
     * 更新我的钱
     */
    static void setMyMoney() {
        User.myMoney = 0;
    }
}
